package com.jrx.anytxn.transaction.job.paph.config;

import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.entity.TlFilePaymentLog;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.bean.PaphFileBean;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.job.paph.step.GenerateRepayDetailFileListener;
import com.jrx.anytxn.transaction.job.paph.step.GenerateRepayDetailFileProcessor;
import com.jrx.anytxn.transaction.job.paph.step.GenerateRepayDetailFileWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuyu
 * @Description 生成银行扣款结果分库文件
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateRepayDetailFileConfiguration {

	@Resource(name = "paramSqlSessionFactory")
	SqlSessionFactory paramSqlSessionFactory;
	@Autowired
	private JobBuilderFactory jobBuilderFactory;
	@Autowired
	private StepBuilderFactory stepBuilderFactory;
	@Autowired
	private IOrganizationTableService organizationTableService;
	@Autowired
	private GenerateRepayDetailFileListener generateRepayDetailFileListener;
	@Autowired
	private AnytxnBatchProperties anytxnBatchProperties;
	@Autowired
	private GenerateRepayDetailFileProcessor generateBankDeductionFileProcessor;
	@Autowired
	private GenerateRepayDetailFileWriter generateBankDeductionFileWriter;

	@Bean
	public Job generateRepayDetailFileJob(){
		return jobBuilderFactory.get("generateRepayDetailFileJob")
				.listener(generateRepayDetailFileListener)
				.start(generateRepayDetailFileStep())
				.build();
	}

	@Bean
	public Step generateRepayDetailFileStep() {
		return stepBuilderFactory.get("generateRepayDetailFileStep")
				.<TlFilePaymentLog, PaphFileBean>chunk(anytxnBatchProperties.getGenerateBankDeductionFileConfig().getChunkLimit())
				.reader(generateBankDeductionFileReader())
				.processor(generateBankDeductionFileProcessor)
				.writer(generateBankDeductionFileWriter)
				.taskExecutor(new SimpleAsyncTaskExecutor())
				.throttleLimit(anytxnBatchProperties.getGenerateBankDeductionFileConfig().getThrottleLimit())
				.build();
	}


    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlFilePaymentLog> generateBankDeductionFileReader() {
        MyBatisCursorItemReader<TlFilePaymentLog> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(paramSqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.param.mapper.ext.ExtTlFilePaymentLogMapper.selectPaymentLogForBatch");
        Map<String, Object> map = new HashMap<>();

        PrOrganizationTable organizationTable = organizationTableService.findByOrgIdForBatch(Constant.DEFAULT_ORG_NUMBER);
        map.put("tradeDate",organizationTable.getToday());
        map.put("tradeFlagList", Arrays.asList(TransBizConstant.FILE_TRADE_FLAG_0,TransBizConstant.FILE_TRADE_FLAG_1,TransBizConstant.FILE_TRADE_FLAG_2));

        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<TlFilePaymentLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }
}
